package com.gotop.system.wrapper;

import com.gotop.common.constant.CommonConstant;
import com.gotop.common.node.ForestNodeMerger;
import com.gotop.common.utils.ApplicationStringUtils;
import com.gotop.common.utils.support.BeanUtil;
import com.gotop.entity.system.Menu;
import com.gotop.system.service.system.IDictService;
import com.gotop.system.service.system.IMenuService;
import com.gotop.vo.system.MenuVO;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 包装类,返回视图层所需的字段
 *
 * @author superfish
 */
public class MenuWrapper extends BaseEntityWrapper<Menu, MenuVO> {

    private static IMenuService menuService;

    private static IDictService dictService;

    static {
        menuService = ApplicationStringUtils.getBean(IMenuService.class);
        dictService = ApplicationStringUtils.getBean(IDictService.class);
    }

    public static MenuWrapper build() {
        return new MenuWrapper();
    }

    @Override
    public MenuVO entityVO(Menu menu) {
        MenuVO menuVO = BeanUtil.copy(menu, MenuVO.class);
        if (menu.getParentId() == CommonConstant.TOP_PARENT_ID) {
            menuVO.setParentName(CommonConstant.TOP_PARENT_NAME);
        } else {
            Menu parent = menuService.getById(menu.getParentId());
            menuVO.setParentName(parent.getName());
        }
        menuVO.setCategoryName(dictService.getValue("menu_category", menuVO.getCategory()));
        menuVO.setActionName(dictService.getValue("button_func", menuVO.getAction()));
        menuVO.setIsOpenName(dictService.getValue("yes_no", menuVO.getIsOpen()));
        return menuVO;
    }


    public List<MenuVO> listNodeVO(List<Menu> list) {
        List<MenuVO> collect = list.stream().map(menu -> BeanUtil.copy(menu, MenuVO.class)).collect(Collectors.toList());
        return ForestNodeMerger.merge(collect);
    }

}
